<?php

declare(strict_types=1);

namespace magein\laravel\seecms;

use Illuminate\Support\Facades\Route;
use magein\laravel\seecms\command\Seed;
use magein\seecms\Factory;
use magein\seecms\SeeLang;
use magein\seecms\SeeRequest;
use magein\laravel\seecms\command\Database;
use magein\laravel\seecms\command\Publish;
use magein\laravel\seecms\command\Vendor;
use Illuminate\Support\ServiceProvider;

class SeeServiceProvider extends ServiceProvider
{

    public function register()
    {

    }

    public function boot(Route $route): void
    {
        // 设置配置文件
        $config = config('seecms');
        Factory::config()->setConfig($config);

        //设置语言包
        $support = Factory::config()->lang->support();
        $cookie_var = Factory::config()->lang->cookieVar();
        if ($cookie_var) {
            $lang = request()->cookie($cookie_var, '');
        } else {
            $header_var = Factory::config()->lang->headerVar();
            $lang = $header_var ? request()->header($header_var) : '';
        }

        if (empty($lang) || empty($support[$lang] ?? '')) {
            $lang = 'zh-cn';
        }

        SeeLang::instance()->switchLangSet($lang);
        SeeLang::instance()->load(app_path() . '/admin/lang/' . $lang . '.php');

        // 设置请求参数
        SeeRequest::instance()->set(request()->method(), request()->input());

        // 添加命令
        $this->commands(
            [
                'seecms:publish' => Publish::class,
                'seecms:database' => Database::class,
                'seecms:seed' => Seed::class,
                'seecms:vendor' => Vendor::class,
            ]
        );

        $this->loadMigrationsFrom(__DIR__ . '/../database/migrations');
    }
}
